package com.ozo.cloud.gen.util.sql;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * mysql建表工具
 *
 * @author qiangesoft
 * @date 2024-11-14
 */
public class MySQLCreateTableBuilder {

    /**
     * 表名
     */
    private String tableName;

    /**
     * 表字段
     */
    private List<Column> columns = new ArrayList<>();

    /**
     * 存储引擎
     */
    private String engine = "InnoDB";

    /**
     * 字符集
     */
    private String characterSet = "utf8mb4";

    /**
     * 字符排序规则
     */
    private String collation = "utf8mb4_general_ci";

    /**
     * 表注释
     */
    private String tableComment;

    public MySQLCreateTableBuilder setTableName(String tableName) {
        this.tableName = tableName;
        return this;
    }

    public MySQLCreateTableBuilder setTableComment(String tableComment) {
        this.tableComment = tableComment;
        return this;
    }

    public MySQLCreateTableBuilder addColumn(Column column) {
        this.columns.add(column);
        return this;
    }

    public MySQLCreateTableBuilder setCharacterSet(String characterSet) {
        this.characterSet = characterSet;
        return this;
    }

    public MySQLCreateTableBuilder setCollation(String collation) {
        this.collation = collation;
        return this;
    }

    public MySQLCreateTableBuilder setEngine(String engine) {
        this.engine = engine;
        return this;
    }

    public String build() {
        StringBuilder sb = new StringBuilder("CREATE TABLE ").append(tableName).append(" (\n");
        for (int i = 0; i < columns.size(); i++) {
            sb.append(columns.get(i));
            if (i < columns.size() - 1) {
                sb.append(",\n");
            }
        }
        sb.append("\n) ENGINE = ").append(engine)
                .append(" CHARACTER SET = ").append(characterSet).append(" COLLATE = ").append(collation)
                .append(" COMMENT = '").append(tableComment).append("'")
                .append(" ROW_FORMAT = Dynamic;");
        return sb.toString();
    }

    /**
     * 字段
     */
    public static class Column {

        private String name;

        private String comment;

        private String type;

        private Integer dataLength;

        private Integer decimalLength;

        private boolean nullable;

        private String defaultValue;

        private boolean primaryKey;

        private boolean autoIncrement;

        public Column(String name, String comment, String type, Integer dataLength, Integer decimalLength, boolean nullable, String defaultValue, boolean primaryKey, boolean autoIncrement) {
            this.name = name;
            this.comment = comment;
            this.type = type;
            this.dataLength = dataLength;
            this.decimalLength = decimalLength;
            this.nullable = nullable;
            this.defaultValue = defaultValue;
            this.primaryKey = primaryKey;
            this.autoIncrement = autoIncrement;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder(name).append(" ").append(type);
            List<String> decimalList = Arrays.asList("float", "double", "decimal");
            List<String> stringList = Arrays.asList("char", "varchar");
            if (decimalList.contains(type)) {
                if (dataLength != null && decimalLength != null) {
                    sb.append("(").append(dataLength).append(", ").append(decimalLength).append(")");
                }
            } else if (stringList.contains(type)) {
                if (dataLength != null) {
                    sb.append("(").append(dataLength).append(")");
                }
            }
            if (!nullable) {
                sb.append(" NOT NULL");
            }
            if (defaultValue != null) {
                sb.append(" DEFAULT ").append(defaultValue);
            }
            if (primaryKey) {
                sb.append(" PRIMARY KEY");
            }
            if (autoIncrement) {
                sb.append(" AUTO_INCREMENT");
            }
            if (comment != null) {
                sb.append(" COMMENT '").append(comment).append("'");
            }
            return sb.toString();
        }
    }

    public static void main(String[] args) {
        MySQLCreateTableBuilder builder = new MySQLCreateTableBuilder()
                .setTableName("users")
                .addColumn(new Column("id", "id", "INT", null, null, false, null, true, true))
                .addColumn(new Column("username", "用户名", "VARCHAR", 50, null, true, null, false, false))
                .addColumn(new Column("email", "邮箱", "VARCHAR", 100, null, true, null, false, false))
                .addColumn(new Column("created_at", "创建人", "TIMESTAMP", null, null, true, "CURRENT_TIMESTAMP", false, false))
//                .setEngine("InnoDB")
//                .setCharacterSet("utf8mb4")
//                .setCollation("utf8mb4_0900_ai_ci")
                .setTableComment("用户表");

        String sql = builder.build();
        System.out.println(sql);
    }

}